package com.kandinfo.isbnmanage.repository.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.kandinfo.isbnmanage.domian.IsbnInformation;
import com.kandinfo.isbnmanage.domian.IsbnOrder;
import com.kandinfo.isbnmanage.domian.IsbnPage;
import com.kandinfo.isbnmanage.repository.IsbnViewRepository;

/**
 * 
 * @author PatrickChen-TG
 *
 */
@Repository
public class IsbnViewRepositoryImpl implements IsbnViewRepository {

	@Autowired
	private EntityManager entityManager;

	@Override
	@Transactional
	public List<IsbnInformation> findInforByBarcode(String barcode) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("select ii from IsbnInformation ii where ii.barcode=:barcode");
		
		TypedQuery<IsbnInformation> query = entityManager.createQuery(nativeSql.toString(), IsbnInformation.class);
		query.setParameter("barcode", barcode);
		
		return query.getResultList();
	}
	
	@Override
	public List<IsbnInformation> findInforByCond(String barcode, Long isbnInformationId) {
 		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("select ii from IsbnInformation ii where ii.barcode=:barcode");
		if (isbnInformationId != null) {
			nativeSql.append(" and ii.id = :id ");
		}
		
		TypedQuery<IsbnInformation> query = entityManager.createQuery(nativeSql.toString(), IsbnInformation.class);
		query.setParameter("barcode", barcode);
		if (isbnInformationId != null) {
			query.setParameter("id", isbnInformationId);
		}
		return query.getResultList();
	}
	
	@Override
	@Transactional
	public IsbnInformation findInforByInforId(Long isbnInformationId) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("select ii from IsbnInformation ii where ii.id=:isbnInformationId");
		
		TypedQuery<IsbnInformation> query = entityManager.createQuery(nativeSql.toString(), IsbnInformation.class);
		query.setParameter("isbnInformationId", isbnInformationId);
		
		// trace to source code
		if (query.getResultList().size() <= 0) {
			return null;
		} else {
			return query.getSingleResult();
		}
	}

	@Override
	@Transactional
	public IsbnOrder findOrdersByInforId(Long isbnInformationId) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("select io from IsbnOrder io where io.isbnInformationId=:isbnInformationId");
		
		TypedQuery<IsbnOrder> query = entityManager.createQuery(nativeSql.toString(), IsbnOrder.class);
		query.setParameter("isbnInformationId", isbnInformationId);
		
		// trace to source code
		if (query.getResultList().size() <= 0) {
			return null;
		} else {
			return query.getSingleResult();
		}
	}

	@Override
	@Transactional
	public List<IsbnPage> findPagesByInforId(Long isbnInformationId) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("select ip from IsbnPage ip where ip.isbnInformationId=:isbnInformationId");
		
		TypedQuery<IsbnPage> query = entityManager.createQuery(nativeSql.toString(), IsbnPage.class);
		query.setParameter("isbnInformationId", isbnInformationId);
		
		return query.getResultList();
	}

}
